package com.example.myapplication.found;

import com.example.myapplication.found.bean.BuyPointBean;
import com.example.myapplication.found.bean.FoundBean;
import com.example.myapplication.found.bean.KDJOrderBean;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockBean2;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.StockNameBean;
import com.example.myapplication.found.bean.StockOrderBean;
import com.example.myapplication.found.bean.ZhangDieBean;
import com.example.myapplication.found.contract.CghCallback;
import com.example.myapplication.found.contract.FenxiCallback;
import com.example.myapplication.found.contract.KdjCallback;
import com.example.myapplication.found.contract.PointCallback;
import com.example.myapplication.found.contract.SimpleCallback;
import com.example.myapplication.found.contract.ZhangDieCallback;
import com.example.myapplication.found.file.FoundDayJsonFile;
import com.example.myapplication.found.model.ChengJiaoLiang;
import com.example.myapplication.found.model.DaBan;
import com.example.myapplication.found.model.Fenxi0;
import com.example.myapplication.found.model.Fenxi;
import com.example.myapplication.found.model.Fenxi10;
import com.example.myapplication.found.model.Fenxi12;
import com.example.myapplication.found.model.Fenxi13;
import com.example.myapplication.found.model.Fenxi2;
import com.example.myapplication.found.model.Fenxi3;
import com.example.myapplication.found.model.Fenxi4;
import com.example.myapplication.found.model.Fenxi5;
import com.example.myapplication.found.model.Fenxi6;
import com.example.myapplication.found.model.Fenxi7;
import com.example.myapplication.found.model.Fenxi8;
import com.example.myapplication.found.model.Fenxi9;
import com.example.myapplication.found.model.KDJ;
import com.example.myapplication.found.model.Point;
import com.example.myapplication.found.model.Point2;
import com.example.myapplication.found.model.WeekCgh;
import com.example.myapplication.found.model.ZhangDie;
import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.example.myapplication.util.TimeUtil;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2022/2/10
 * @description
 */
public class Stock {
    public static boolean isTest = true;
    public String fundCode;
    public String fundName;
    public String market;
    public StockBean dayStockBean;
    public StockBean weekStockBean;

    SimpleCallback simpleCallback;
    FoundDayJsonFile foundDayJsonFile;
    FoundBean mFoundBean;

    public static volatile Hashtable<String, StockBean> stockBeans = new Hashtable<>();
    public static volatile Hashtable<String, List<StockNameBean>> stockNameBeans = new Hashtable<>();

    public void setSimpleCallback(SimpleCallback simpleCallback) {
        this.simpleCallback = simpleCallback;
    }

    public Stock(String fundCode, String fundName, String market) {
        this.fundCode = fundCode;
        this.fundName = fundName;
        this.market = market;
        foundDayJsonFile = new FoundDayJsonFile();
    }

    public Stock(FoundBean item) {
        this.mFoundBean = item;
        this.fundCode = item.f12;
        this.fundName = item.f14;
        this.market = item.f13;
        foundDayJsonFile = new FoundDayJsonFile();
    }

    public static void main(String[] args) throws Exception {
        Stock stock = new Stock("BK0737", "测试行业", "0");
        stock.start();
    }

    public void lmtData() {
        if (dayStockBean == null
//                || weekStockBean == null
                || fundCode.startsWith("688") || fundCode.startsWith("300")
                || fundName.startsWith("退市")
//                || fundName.startsWith("*ST")
//                || fundName.startsWith("ST")
//                || fundName.startsWith("S")
                || dayStockBean.klineList.size() < (Fenxi13.dateIndex.get() + 250)
//                || !dayStockBean.klineList.get(dayStockBean.klineList.size() - 1).date.equals("2022-08-25")
        ) {
            if (simpleCallback != null) {
                simpleCallback.kdjCallback(null, null);
            }
            return;
        }

        if (dayStockBean.klineList.size() > IndustryFound.lmt) {
            dayStockBean.klineList = dayStockBean.klineList.subList(dayStockBean.klineList.size() - IndustryFound.lmt, dayStockBean.klineList.size());
        }
        for (int i = 0; i < dayStockBean.klineList.size(); i++) {
            dayStockBean.klineList.get(i).index = i;
        }

//        if (weekStockBean.klineList.size() > IndustryFound.lmt) {
//            weekStockBean.klineList = weekStockBean.klineList.subList(weekStockBean.klineList.size() - IndustryFound.lmt, weekStockBean.klineList.size());
//        }
//        for (int i = 0; i < weekStockBean.klineList.size(); i++) {
//            weekStockBean.klineList.get(i).index = i;
//        }
        fenxi();
    }

    public void fenxi() {
        List<StockNameBean> stockNameBeanList = stockNameBeans.get(fundCode);
        if (stockNameBeanList == null) {
            stockNameBeanList = foundDayJsonFile.getStockName(fundCode);
            if (stockNameBeanList != null) {
                Collections.reverse(stockNameBeanList);
                stockNameBeans.put(fundCode, stockNameBeanList);
            }
        }
        switch (IndustryFound.tstType) {
            case 1:
                Point point = new Point(dayStockBean, weekStockBean);
                point.setSimpleCallback(new PointCallback() {
                    @Override
                    public void pointCallback(List<StockOrderBean> orderList) {
                        if (simpleCallback != null) {
                            simpleCallback.pointCallback(orderList);
                        }
                    }
                });
                point.start();
                break;
            case 2:

                break;
            case 3:
                KDJ kdj = new KDJ(dayStockBean, weekStockBean);
                kdj.stockNameBeanList = stockNameBeanList;
                kdj.setKdjCallback(new KdjCallback() {
                    @Override
                    public void kdjCallback(KDJOrderBean kdjOrderBean, BuyPointBean buyPointBean) {
                        if (simpleCallback != null) {
                            simpleCallback.kdjCallback(kdjOrderBean, buyPointBean);
                        }
                    }
                });
                kdj.start();
                break;
            case 4:
                ZhangDie zhangDie = new ZhangDie(dayStockBean);
                float money = zhangDie.start();
                if (simpleCallback != null) {
                    simpleCallback.testModelNext(money);
                }
                break;
            case 5:
                ChengJiaoLiang chengJiaoLiang = new ChengJiaoLiang(dayStockBean, weekStockBean);
                chengJiaoLiang.setSimpleCallback(new PointCallback() {
                    @Override
                    public void pointCallback(List<StockOrderBean> orderList) {
                        if (simpleCallback != null) {
                            simpleCallback.pointCallback(orderList);
                        }
                    }
                });
                chengJiaoLiang.start();
                break;
            case 6:
                Fenxi fenxi = new Fenxi(dayStockBean, weekStockBean);
                fenxi.setZhangDieCallback(new ZhangDieCallback() {
                    @Override
                    public void zhangDieCallback(ZhangDieBean zhangDieBean) {
                        if (simpleCallback != null) {
                            simpleCallback.zhangDieCallback(zhangDieBean);
                        }
                    }
                });
                fenxi.start();
                break;
            case 7:
                WeekCgh weekCgh = new WeekCgh(dayStockBean, weekStockBean);
                weekCgh.setCghCallback(new CghCallback() {
                    @Override
                    public void cghCallback(Map<Integer, Float> map, KDJOrderBean kdjOrderBean) {
                        if (simpleCallback != null) {
                            simpleCallback.cghCallback(map, kdjOrderBean);
                        }
                    }
                });
                weekCgh.start();
                break;
            case 8:
                Point2 point2 = new Point2(dayStockBean, weekStockBean);
                point2.start();
                break;
            case 9:
                DaBan daBan = new DaBan(dayStockBean, weekStockBean);
                daBan.foundBean = mFoundBean;
                daBan.setSimpleCallback(new DaBan.DaBanCallback() {
                    @Override
                    public void daBanCallback() {
                        if (simpleCallback != null) {
                            simpleCallback.daBanCallback();
                        }
                    }
                });
                daBan.start();
                break;
            case 10:
                Fenxi2 fenxi2 = new Fenxi2(dayStockBean, weekStockBean);
                fenxi2.setFenxiCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.daBanCallback();
                        }
                    }
                });
                fenxi2.start();
                break;
            case 11:
                Fenxi3 fenxi3 = new Fenxi3(dayStockBean, weekStockBean);
                fenxi3.setFenxiCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.daBanCallback();
                        }
                    }
                });
                fenxi3.start();
                break;
            case 12:
                Fenxi4 fenxi4 = new Fenxi4(dayStockBean, weekStockBean);
                fenxi4.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi4.start();
                break;
            case 13:
                Fenxi5 fenxi5 = new Fenxi5(dayStockBean, weekStockBean);
                fenxi5.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi5.start();
                break;
            case 14:
                Fenxi6 fenxi6 = new Fenxi6(dayStockBean, weekStockBean);
                fenxi6.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi6.start();
                break;
            case 15:
                Fenxi7 fenxi7 = new Fenxi7(dayStockBean, weekStockBean);
                fenxi7.stockNameBeanList = stockNameBeanList;
                fenxi7.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi7.start();
                break;
            case Fenxi0.type:
                Fenxi0 fenxi0 = new Fenxi0(dayStockBean, weekStockBean);
                fenxi0.foundBean = mFoundBean;
                fenxi0.stockNameBeanList = stockNameBeanList;
                fenxi0.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi0.start();
                break;
            case Fenxi8.type:
                Fenxi8 fenxi8 = new Fenxi8(dayStockBean, weekStockBean);
                fenxi8.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi8.start();
                break;
            case Fenxi9.type:
                Fenxi9 fenxi9 = new Fenxi9(dayStockBean, weekStockBean);
                fenxi9.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi9.start();
                break;
            case Fenxi10.type:
                Fenxi10 fenxi10 = new Fenxi10(dayStockBean, weekStockBean);
                fenxi10.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi10.start();
                break;
            case Fenxi12.type:
                Fenxi12 fenxi12 = new Fenxi12(dayStockBean, weekStockBean);
                fenxi12.setKdjCallback(new FenxiCallback() {
                    @Override
                    public void fenxiCallback(int type) {
                        if (simpleCallback != null) {
                            simpleCallback.fenxiCallback(1);
                        }
                    }
                });
                fenxi12.start();
                break;
        }
    }

    public void start() {
        dayStockBean = stockBeans.get(fundCode);
        if (dayStockBean != null) {
            lmtData();
        } else if (!IndustryFound.isUpdateData && (dayStockBean = foundDayJsonFile.getStock(fundCode, 1)) != null
//                && (weekStockBean = foundDayJsonFile.getStock(fundCode, 2)) != null
        ) {
            stockBeans.put(fundCode, dayStockBean);
            lmtData();
        } else {
            String startTime = TimeUtil.getStatusTimeByTimeMillis(System.currentTimeMillis(), "yyyyMMdd");
            startTime = "0";
//            getKlines("https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=" + startTime + "&end=20500101&ut=fa5fd1943c7b386f172d6893dbfba10b&rtntype=6&" +
//                    "secid=" + market + "." + fundCode + "&klt=101&fqt=0&cb=jsonp1645673082202", 1);

            String foundUrl = "https://push2his.eastmoney.com/api/qt/stock/kline/get?secid=" + market + "." + fundCode + "&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&lmt=25000&klt=101&fqt=1&end=30000101&ut=fa5fd1943c7b386f172d6893dbfba10b&cb=cb_1662086438298_23991038&cb_1662086438298_23991038=cb_1662086438298_23991038";
            getKlines(foundUrl, 1);

//            getKlines("https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&ut=fa5fd1943c7b386f172d6893dbfba10b&rtntype=6&" +
//                    "secid=" + market + "." + fundCode + "&klt=101&fqt=1&cb=jsonp1645673082202", 1);

//            getKlines("http://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&ut=fa5fd1943c7b386f172d6893dbfba10b&rtntype=6&" +
//                    "secid=" + market + "." + fundCode + "&klt=102&fqt=1&cb=jsonp1645705983849", 2);
        }


//        getKlines("https://53.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery33105761660556846244_1644477014134&secid=SH." + fundCode
//                + "&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=101&fqt=1&end=20500101&" +
//                "lmt=" + IndustryFound.lmt + "&_=" + System.currentTimeMillis(), 1);

//        getKlines("https://53.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery33105761660556846244_1644477014134&secid=90." + fundCode
//                + "&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=102&fqt=1&beg=0&end=20500101&smplmt=1419&" +
//                "lmt=" + IndustryFound.lmt + "&_=" + System.currentTimeMillis(), 2);

        if (foundDayJsonFile.getStockName(fundCode) == null) {
            String market2;
            if (market.equals("1")) {
                market2 = "SH";
            } else {
                market2 = "SZ";
            }
            String stockNameUrl = "http://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery35107499928198326191_1661345873978&reportName=RPT_IPO_ABSTOCK&columns=SECURITY_CODE%2CCHANGE_DATE%2CCHANGE_AFTER_FN%2CCHANGE_AFTER_AB%2CTRADE_MARKET_TYPE%2CRANK%2CSECUCODE&quoteColumns=&filter=(SECUCODE%3D%22" + fundCode + "." + market2 + "%22)&pageNumber=1&pageSize=100&sortTypes=1&sortColumns=CHANGE_DATE&source=QuoteWeb&client=WEB&_=1661345873979";
            getStockName(stockNameUrl);
        }
    }

    public void getKlines(String url, int type) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("Referer", "http://fundf10.eastmoney.com/")
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    String tag1 = "\"data\":";
                    int index1 = html.indexOf(tag1);
                    int index2 = html.lastIndexOf("});");
                    html = html.substring(index1 + tag1.length(), index2);
                    StockBean2 stockBean = GsonUtil.parse(html, StockBean2.class);
                    if (stockBean == null) {
                        System.out.println("sss" + url);
                    }
                    List<StockKlineBean> klineList = new ArrayList<>();
                    for (int i = 0; i < stockBean.klines.size(); i++) {
                        String line = stockBean.klines.get(i);
                        String[] item = line.split(",");
                        StockKlineBean stockKlineBean = new StockKlineBean();
                        stockKlineBean.date = item[0];
                        stockKlineBean.date2 = TimeUtil.getTimeSS(item[0] + " 00:00:00");
                        stockKlineBean.jinKai = Float.parseFloat(item[1]);
                        stockKlineBean.shouPan = Float.parseFloat(item[2]);
                        stockKlineBean.zuigao = Float.parseFloat(item[3]);
                        stockKlineBean.zuidi = Float.parseFloat(item[4]);
                        stockKlineBean.chengjiaoliang = Double.parseDouble(item[5]);
                        stockKlineBean.chengjiaoe = Double.parseDouble(item[6]);
                        stockKlineBean.zhenfu = Float.parseFloat(item[7]);
                        stockKlineBean.chg = Float.parseFloat(item[8]);
                        stockKlineBean.zde = Float.parseFloat(item[9]);
                        stockKlineBean.huanshou = Float.parseFloat(item[10]);
                        stockKlineBean.index = i;
                        klineList.add(stockKlineBean);
                    }
                    stockBean.klineList = klineList;
                    if (type == 1) {
                        dayStockBean = stockBean;
                        foundDayJsonFile.saveStock(fundCode, dayStockBean, type);
                    } else {
                        weekStockBean = stockBean;
                        foundDayJsonFile.saveStock(fundCode, weekStockBean, type);
                    }
                    lmtData();
                }
            }
        });
    }

    public void getStockName(String url) {

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("Referer", "http://fundf10.eastmoney.com/")
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    String tag1 = "\"data\":";
                    int index1 = html.indexOf(tag1);
                    int index2 = html.lastIndexOf("}]");
                    try {
                        html = html.substring(index1 + tag1.length(), index2 + 2);
                        List<StockNameBean> stockBean = GsonUtil.parseList(html, new TypeToken<List<StockNameBean>>() {
                        }.getType());
                        for (StockNameBean bean : stockBean) {
                            long date = TimeUtil.getTimeSS(bean.getCHANGE_DATE());
                            bean.setDate(date);
                        }
                        foundDayJsonFile.saveStockName(fundCode, stockBean);
                    }catch (Exception e){

                    }
                }
            }
        });
    }

}
